<?php
/**
 * @version 2014-08-07
 * @author Norman
 * For Stock Adjustment  to remember this issue.
 */
class Model_Products_Adjustment extends Zend_Db_Table_Abstract {

	protected $_name = 'products_v1_adjust';

	/**
	 * Get Adjust by ID
	 * @param unknown_type $idAdjust
	 */
	public function getAdjustment( $idAdjust){

		$row = $this->fetchRow("`id_adjust` = ". $idAdjust);
		if(!$row) return false;
		return $row->toArray();

	}
	/**
	 * Use this one to Initial , no cancel information needed
	 * @param unknown_type $codeShop
	 * @param unknown_type $codeProduct
	 * @param unknown_type $qtySys
	 * @param unknown_type $qtyAct
	 * @param unknown_type $qtyAdjust
	 * @param unknown_type $qtyOrder
	 * @param unknown_type $dateAdd
	 * @param unknown_type $timeAdd
	 */
	public function initAdjustment( $codeShop , $codeProduct , $qtySys , $qtyAct , $qtyAdjust , $qtyOrder , $dateAdd , $timeAdd ){

		$data = array(
					
				"code_shop" =>  $codeShop ,
				"code_product" =>  $codeProduct ,
				"qty_sys" =>  $qtySys ,
				"qty_act" =>  $qtyAct ,
				"qty_adjust" =>  $qtyAdjust ,
				"qty_order" =>  $qtyOrder ,
				"date_add" =>  $dateAdd ,
				"time_add" =>  $timeAdd
		
		);
		$this->insert($data);		
		
	}
	
	/**
	 * Generateed function, Add Adjustment whole
	 * @param unknown_type $codeShop
	 * @param unknown_type $codeProduct
	 * @param unknown_type $qtySys
	 * @param unknown_type $qtyAct
	 * @param unknown_type $qtyAdjust
	 * @param unknown_type $qtyOrder
	 * @param unknown_type $dateAdd
	 * @param unknown_type $timeAdd
	 * @param unknown_type $isCancel
	 * @param unknown_type $cancelBy
	 * @param unknown_type $cancelDate
	 * @param unknown_type $noKa
	 * @param unknown_type $noOrder
	 */
	public function addAdjustment( $codeShop , $codeProduct , $qtySys , $qtyAct , $qtyAdjust , $qtyOrder , $dateAdd , $timeAdd , $isCancel , $cancelBy , $cancelDate , $noKa , $noOrder){

		$data = array(
					
				"code_shop" =>  $codeShop ,
				"code_product" =>  $codeProduct ,
				"qty_sys" =>  $qtySys ,
				"qty_act" =>  $qtyAct ,
				"qty_adjust" =>  $qtyAdjust ,
				"qty_order" =>  $qtyOrder ,
				"date_add" =>  $dateAdd ,
				"time_add" =>  $timeAdd ,
				"is_cancel" =>  $isCancel ,
				"cancel_by" =>  $cancelBy ,
				"cancel_date" =>  $cancelDate ,
				"no_ka" =>  $noKa ,
				"no_order" =>  $noOrder

		);
		$this->insert($data);

	}
	
	/**
	 * Update the adjustment , generated function , no need 
	 * @param unknown_type $idAdjust
	 * @param unknown_type $codeShop
	 * @param unknown_type $codeProduct
	 * @param unknown_type $qtySys
	 * @param unknown_type $qtyAct
	 * @param unknown_type $qtyAdjust
	 * @param unknown_type $qtyOrder
	 * @param unknown_type $dateAdd
	 * @param unknown_type $timeAdd
	 * @param unknown_type $isCancel
	 * @param unknown_type $cancelBy
	 * @param unknown_type $cancelDate
	 * @param unknown_type $noKa
	 * @param unknown_type $noOrder
	 */

	public function updateAdjustment(  $idAdjust ,  $codeShop , $codeProduct , $qtySys , $qtyAct , $qtyAdjust , $qtyOrder , $dateAdd , $timeAdd , $isCancel , $cancelBy , $cancelDate , $noKa , $noOrder){
		$data = array(
					
				"code_shop" =>  $codeShop ,
				"code_product" =>  $codeProduct ,
				"qty_sys" =>  $qtySys ,
				"qty_act" =>  $qtyAct ,
				"qty_adjust" =>  $qtyAdjust ,
				"qty_order" =>  $qtyOrder ,
				"date_add" =>  $dateAdd ,
				"time_add" =>  $timeAdd ,
				"is_cancel" =>  $isCancel ,
				"cancel_by" =>  $cancelBy ,
				"cancel_date" =>  $cancelDate ,
				"no_ka" =>  $noKa ,
				"no_order" =>  $noOrder

		);
			
		$this->update($data,"`id_adjust` = ". $idAdjust);
	}
	/**
	 * Remove the adjustment by ID
	 * @param unknown_type $idAdjust
	 */
	public function deleteAdjustment( $idAdjust){

		$this->delete("`id_adjust` = ". $idAdjust);

	}
	/**
	 * Manager Cancel the Adjustment , remember canceled Date and By Whol
	 * @param unknown_type $idAdjust
	 * @param unknown_type $idManager
	 * @param unknown_type $cancelDate
	 */
	public function cancelAdjustment($idAdjust,$idManager,$cancelDate){
		$data = array(
					
				"is_cancel" =>  1 ,
				"cancel_by" =>  $idManager ,
				"cancel_date" =>  $cancelDate
		);
			
		$this->update($data,"`id_adjust` = ". $idAdjust);		
		
		
	}
	/**
	 * Manager Update Qty of Adjustment 
	 * @param unknown_type $idAdjust
	 * @param unknown_type $qtyOrder
	 */
	public function updateOrderQty($idAdjust,$qtyOrder){
		
		$data = array(
				"qty_order" =>  $qtyOrder
		);
			
		$this->update($data,"`id_adjust` = ". $idAdjust);		
		
		
	}
	/**
	 * Generated List all Function 
	 */
	public function listAll(){

		$rows =$this->fetchAll("1","id_adjust DESC ");
		if(!(bool)$rows) return false;
		return $rows->toArray();

	}
	/**
	 * List All Pendding for Freeze
	 * @param unknown_type $codeShop
	 * @param unknown_type $dateBegin
	 * @param unknown_type $dateEnd
	 */
	public function listPendingAdjustment($codeShop,$dateBegin,$dateEnd){
		
		$whereStr = "`code_shop` LIKE '{$codeShop}' AND `no_ka` IS NULL AND `date_add` >= '{$dateBegin}' AND `date_add` <= '{$dateEnd}' AND `is_cancel` IS NULL ";
		$rows = $this->fetchAll($whereStr,"date_add");
		
		if(!(bool)$rows->toArray()) return false;
		return $rows->toArray();
	}
	/**
	 * Confirmed , not temp
	 * @param unknown_type $codeShop
	 * @param unknown_type $dateBegin
	 * @param unknown_type $dateEnd
	 */
	public function listConfirmAdjustment($codeShop,$dateBegin,$dateEnd){
	
		$whereStr = "`code_shop` LIKE '{$codeShop}' AND `no_ka` IS NOT NULL AND `date_add` >= '{$dateBegin}' AND `date_add` <= '{$dateEnd}' AND `is_cancel` IS NULL ";
		$rows = $this->fetchAll($whereStr,"date_add");
	
		if(!(bool)$rows->toArray()) return false;
		return $rows->toArray();
	}
	/**
	 * Freeze it 
	 * @param unknown_type $idAdjust
	 * @param unknown_type $noKa
	 * @param unknown_type $noOrder
	 */
	public function updateTempKa($idAdjust,$noKa,$noOrder){
		
		$data = array(
				"no_ka" =>  $noKa ,
				"no_order" =>  $noOrder,
				"date_freeze" => Model_DatetimeHelper::dateToday()
				);
		$this->update($data,"`id_adjust` = ". $idAdjust);		
	}
	/**
	 * List All temped ,don't care which shop why
	 */
	public function listAllTempKa(){
		
		$whereStr = "`no_ka` LIKE 'TMPA%' AND `is_cancel` IS NULL";
		$rows = $this->fetchAll($whereStr,"date_add");
		if(!(bool)$rows->toArray()) return false;
		return $rows->toArray();	
			
	}
	/**
	 * Anything Not Adjusted should be not in the stock take list 
	 * here is a problem , if KA not active ? what to do?
	 * 
	 */
	public function listAllNotAdjusted(){
		
		$whereStr = "`no_ka` NOT LIKE 'KA%' AND `is_cancel` IS NULL";
		$rows = $this->fetchAll($whereStr,"date_add");
		if(!(bool)$rows->toArray()) return false;
		return $rows->toArray();
		
	}
	/**
	 * list Temp KA by Shop
	 * @param unknown_type $codeShop
	 */
	public function listTempKaByShop($codeShop){
		
		$whereStr = " (`no_ka` LIKE 'TMPA%' OR `no_order` LIKE 'PD%') AND `is_cancel` IS NULL AND  `code_shop` LIKE '{$codeShop}' ";
		$rows = $this->fetchAll($whereStr,"id_adjust");
		if(!(bool)$rows->toArray()) return false;
		return $rows->toArray();
		
	}
	/**
	 * find all this order and not canceled 
	 * it is PD order with shop code , so no need to identity shop
	 * @param unknown_type $orderNo
	 */
	public function listSpecialOrder($orderNo){
		$whereStr = "`no_order` LIKE '{$orderNo}' AND `is_cancel` IS NULL";
		$rows = $this->fetchAll($whereStr,"code_product");
		if(!(bool)$rows->toArray()) return false;
		return $rows->toArray();		
		
	}
	
	public function updateOrderNo($idAdjust,$noOrder){
		
		$data = array(
				"no_order" => $noOrder
				);
		$this->update($data,"`id_adjust` = ". $idAdjust);
	}
	
	public function updateKaNo($idAdjust,$noKa){
		
		$data = array(
				"no_ka" => $noKa
		);
		$this->update($data,"`id_adjust` = ". $idAdjust);		
		
	}
	public function allFreezedThisWeek($dateFreeze,$shop){
		
		$whereStr = "`date_freeze` = '{$dateFreeze}' AND `is_cancel` IS NULL AND `code_shop` LIKE '{$shop}'  ";
		$rows = $this->fetchAll($whereStr,"id_adjust");
		if(!(bool)$rows->toArray()) return false;
		return $rows->toArray();
		
	}
}



?>